package com.zmkm.controller.warehouse;

import com.zmkm.common.Const;
import com.zmkm.common.ServerResponse;
import com.zmkm.entity.Housekeeper;
import com.zmkm.service.IProductService;
import com.zmkm.service.IService_strongholdService;
import com.zmkm.service.IUserService;
import com.zmkm.service.IWarehouseManageService;
import com.zmkm.vo.Put_in_warehouseRegister;
import com.zmkm.vo.export_warehouseRegister;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({ "/warehouse_manage/" })
public class WarehouseManage {
	@Autowired
	private IProductService iProductService;
	@Autowired
	private IService_strongholdService iService_strongholdService;
	@Autowired
	private IWarehouseManageService iWarehouseManageService;
	@Autowired
	private IUserService iUserService;

	@RequestMapping(value = { "put_in" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView put_in(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.WARE).getModel().get("serverResponse");
		if (response.getStatus() != 0) {
			return GradeVerify(request, Const.WARE);
		}
		mv.setViewName("admin_warehouse/replenish");
		return mv;
	}

	@RequestMapping(value = { "put_in_Register" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView put_in_Register(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.WARE).getModel().get("serverResponse");
		if (response.getStatus() != 0) {
			return GradeVerify(request, Const.WARE);
		}
		ServerResponse responseNew = this.iWarehouseManageService.put_inRegister(pwrInsert(request));
		mv.addObject("submitReturn", responseNew);
		mv.addObject("returnWeb", "admin/warehouse_put_in");
		mv.setViewName("admin_warehouse/submitReturn");
		return mv;
	}

	private Put_in_warehouseRegister pwrInsert(HttpServletRequest request) {
		Put_in_warehouseRegister pwr = new Put_in_warehouseRegister();
		String time = request.getParameter("time").trim();
		String warehouse_address = request.getParameter("warehouse_address").trim();
		String product_name = request.getParameter("product_name").trim();
		String product_model = request.getParameter("product_model").trim();
		int product_type = Integer.parseInt(request.getParameter("product_type").trim());
		String specification = null;
		String unit = null;
		String put_in_price = request.getParameter("put_in_price").trim();
		int quantity = Integer.parseInt(request.getParameter("quantity").trim());

		String sign_for_man = request.getParameter("sign_for_man").trim();
		String delive_goods_man = request.getParameter("delive_goods_man").trim();
		String country_of_origin = request.getParameter("country_of_origin").trim();
		String remarks = request.getParameter("remarks").trim();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			pwr.setPut_in_warehouseRegister(df.parse(time), warehouse_address, product_name, product_model,
					product_type, specification, unit, put_in_price, quantity, null, sign_for_man, delive_goods_man,
					country_of_origin, remarks);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return pwr;
	}

	@RequestMapping(value = { "put_in_select" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView put_in_select(HttpServletRequest request) {
		return null;
	}

	@RequestMapping(value = { "export" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView export(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.WARE).getModel().get("serverResponse");
		if (response.getStatus() != 0) {
			return GradeVerify(request, Const.WARE);
		}
		mv.setViewName("admin_warehouse/export");
		return mv;
	}

	@RequestMapping(value = { "export_register" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView exportRegister(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.WARE).getModel().get("serverResponse");
		if (response.getStatus() != 0) {
			return GradeVerify(request, Const.WARE);
		}
		ServerResponse responseNew = this.iWarehouseManageService.export_Register(ewInsert(request));
		mv.addObject("submitReturn", responseNew);
		mv.addObject("returnWeb", "admin/warehouse_put_in");
		mv.setViewName("admin_warehouse/submitReturn");
		return mv;
	}

	private export_warehouseRegister ewInsert(HttpServletRequest request) {
		export_warehouseRegister ewr = new export_warehouseRegister();
		String time = request.getParameter("time").trim();
		String warehouse_address = request.getParameter("warehouse_address").trim();
		String product_name = request.getParameter("product_name").trim();
		String product_model = request.getParameter("product_model").trim();
		String specification = null;
		String unit = null;
		String export_price = request.getParameter("export_price").trim();
		int quantity = Integer.parseInt(request.getParameter("quantity").trim());
		String export_warehouse_man = request.getParameter("export_warehouse_man").trim();
		String fetch_warehouse_man = request.getParameter("fetch_warehouse_man").trim();
		String remarks = request.getParameter("remarks").trim();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			ewr.setNewExport_warehouseRegister(df.parse(time), warehouse_address, product_name, product_model,
					specification, unit, export_price, quantity, export_warehouse_man, fetch_warehouse_man, remarks);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return ewr;
	}

	@RequestMapping(value = { "export_select" }, produces = { "application/json;charset=utf-8" })
	public ModelAndView export_select() {
		return null;
	}

	@ResponseBody
	@RequestMapping(value = { "warehouse_address_select" }, produces = { "application/json;charset=utf-8" })
	public ServerResponse addressSelect() {
		List<String> list = this.iService_strongholdService.getServiceName(1);
		StringBuffer view = new StringBuffer();
		for (String l : list) {
			view.append("<option>");
			view.append(l);
			view.append("</option>");
		}
		return ServerResponse.createBySuccessMessage(view.toString());
	}

	@ResponseBody
	@RequestMapping(value = { "product_name_select" }, produces = { "application/json;charset=utf-8" })
	public ServerResponse productNameSelect() {
		List<String> list = this.iProductService.productNameByType(99);
		StringBuffer view = new StringBuffer();
		for (String l : list) {
			view.append("<option>");
			view.append(l);
			view.append("</option>");
		}
		return ServerResponse.createBySuccessMessage(view.toString());
	}

	@ResponseBody
	@RequestMapping(value = { "export_product_select" }, produces = { "application/json;charset=utf-8" })
	public ServerResponse export_product_select(HttpServletRequest request) {
		String warehouse_address = request.getParameter("warehouse_address");
		List<String> list = this.iWarehouseManageService.productSelect(warehouse_address);
		StringBuffer view = new StringBuffer();
		for (String l : list) {
			view.append("<option>");
			view.append(l);
			view.append("</option>");
		}
		return ServerResponse.createBySuccessMessage(view.toString());
	}

	@ResponseBody
	@RequestMapping(value = { "product_model_select" }, produces = { "application/json;charset=utf-8" })
	public ServerResponse productModelSelect(HttpServletRequest request) {
		String productName = request.getParameter("productName");
		List<String> list = this.iProductService.productModel(productName);
		StringBuffer view = new StringBuffer();
		for (String l : list) {
			view.append("<option>");
			view.append(l);
			view.append("</option>");
		}
		return ServerResponse.createBySuccessMessage(view.toString());
	}

	public ModelAndView GradeVerify(HttpServletRequest request,ArrayList<Integer> grade) {
		ModelAndView mv = new ModelAndView();
		Housekeeper data = (Housekeeper) request.getSession().getAttribute("housekeeper");
		if (data == null) {
			mv.addObject("serverResponse", ServerResponse.createByErrorMessage("未登陆，请登陆后在进行操作！"));
			mv.setViewName("login");
			return mv;
		}
		if (data != null) {
			ServerResponse verify = this.iUserService.userNameAndpassWordVerify(data.getUserName(), data.getPassWord());
			if (verify.getStatus() == 1) {
				mv.addObject("serverResponse", verify);
				mv.setViewName("login");
				return mv;
			}
			if (!(grade.contains(data.getUser_grade()) || Const.ADMAX.contains(data.getUser_grade())) ) {
				mv.addObject("serverResponse", ServerResponse.createByErrorMessage("没有这个操作权限!"));
				request.getSession().setAttribute("housekeeper", data);
				mv.setViewName("index");
				return mv;
			}
		}
		mv.addObject("serverResponse", ServerResponse.createBySuccess());
		return mv;
	}
}
